<?php

include_once('lib.php');

if(!isset($_GET['file'])) die('Expected file parameter in the URL.');

$file = sanitiseStringToAlphaNumeric($_GET['file']);

$format = null;

if(isset($_GET['format'])) $format = sanitiseStringToAlphaNumeric($_GET['format']);

$path = 'pages/'.$file.'.page';

if(!file_exists($path)) die('No such file at '.$path);

$currentXml = file_get_contents($path);

if(!$format) {
	header('Content-type: text/xml');
	die($currentXml);
}

pilferpage_process($format, $currentXml, true);

function pilferpage_process($format, $xml, $output = false) {
  $errors = array();

  if(!$format) {
    header('Content-type: text/xml');
    die($xml);
  }

  $pipeline_path = 'format/to-' . $format . '/index.pipeline';

  if(!file_exists($pipeline_path)) {
    die('Pipeline file not found at ' . $pipeline_path);
  }

  $pipeline = simplexml_load_file($pipeline_path);
  $archive = array();

  foreach($pipeline as $stage) {
    $node_name = $stage->getName();

    switch($node_name) {
      case 'xslt':
      case 'file':
        if($node_name == 'xslt') {
          $path_to_xsl = realpath(dirname($pipeline_path).'/'.$stage->attributes()->path);
          $result = transform($xml, $path_to_xsl);
        }
        elseif($node_name == 'file') {
            $result = file_get_contents(realpath(dirname($pipeline_path).'/'.$stage->attributes()->path));
        }
        $output_path = $stage->attributes()->archivePath;
        if($output_path) {
          $archive[(string)$output_path] = $result;
        }
        if(!$stage->attributes()->retainCurrentXml) {
          $xml = $result;
        }
        break;

      case 'debug':
        header('Content-Type: text/xml');
        if($stage->attributes()->archivePath) {
          die($archive[(string)$stage->attributes()->archivePath]);
        }
        die($xml);

      case 'remove-namespaces':
        $xml = str_replace(' xmlns="http://www.w3.org/1999/xhtml"', '', $xml);
        break;

      case 'content-type':
        if($output) {
          header('Content-Type: '.$stage->attributes()->mime);
        }
        break;

      case 'download-as':
        if($output) {
          header('Content-Disposition: attachment; filename="'.basename($file).'.'.$stage->attributes()->fileExtension.'"');
        }
        break;

      default:
        die('Unknown pipeline stage of ' . $node_name . ' (from ' . $pipeline_path . ')');
    }
  }

  if($output) {
    if(count($archive)) {
      $zip = new zipfile();
      foreach($archive as $filename => $data) {
        $zip->addFile($data, $filename);
      }

      print $zip->file();
    }
    else{
      print($xml);
    }
    exit;
  }

  if($xml) {
    return $xml;
  }
}

/*
$pipelinePath = 'format/to-'.$format.'/index.pipeline';

if(!file_exists($pipelinePath)) die('No such file at '.$pipelinePath);

$pipeline = simplexml_load_file($pipelinePath);

$archive = Array();

foreach($pipeline as $stage)
	{
	$nodeName = $stage->getName();
	switch($nodeName)
		{
		case 'xslt':
		case 'file':
			if($nodeName == 'xslt')
				{
				$pathToXsl = realpath(dirname($pipelinePath).'/'.$stage->attributes()->path);
				$result = transform($currentXml, $pathToXsl);
				}
			elseif($nodeName == 'file')
				{
				$result = file_get_contents(realpath(dirname($pipelinePath).'/'.$stage->attributes()->path));
				}
			$outputPath = $stage->attributes()->archivePath;
			if($outputPath)
				{
				$archive[(string)$outputPath] = $result;
				}
			if(!$stage->attributes()->retainCurrentXml)
				{
				$currentXml = $result;
				}
			break;
		case 'debug':
			header('Content-Type: text/xml');
			if($stage->attributes()->archivePath)
				{
				die($archive[(string)$stage->attributes()->archivePath]);
				}
			die($currentXml);
		case 'remove-namespaces':
			$currentXml = str_replace(' xmlns="http://www.w3.org/1999/xhtml"', '', $currentXml);
			break;
		case 'content-type':
			header('Content-Type: '.$stage->attributes()->mime);
			break;
		case 'download-as':
			header('Content-Disposition: attachment; filename="'.basename($file).'.'.$stage->attributes()->fileExtension.'"');
			break;
		default:
			die('Unknown pipeline stage of '.$nodeName.' (from '.$pipelinePath.')');
		}

	}
if(count($archive))
	{
	$zip = new zipfile();
	foreach($archive as $filename => $data)
		{
		$zip->addFile($data, $filename);
		}
	
	die($zip->file());
	}
print $currentXml; */

?>
